In Object-Oriented Modeling, State Machine Diagrams describe how an object changes over its lifespan in response to events. This tutorial covers the fundamental building blocks: States and Transitions.
In UML, a state is rendered as a rounded rectangle. It is divided into compartments to show the state's name and internal activities.
| Activity Type | Trigger Moment | Description |
|---|---|---|
entry / action |
On Entering | Executes immediately when the object enters the state. Cannot be interrupted. |
do / activity |
While In State | An ongoing behavior performed while the object remains in the state. Can be interrupted by transitions. |
exit / action |
On Exiting | Executes immediately before the object leaves the state. Cannot be interrupted. |
A transition is represented as a solid directed line connecting a source state to a target state.
The trigger that fires the transition (e.g., buttonClicked or tempChanged).
A Boolean expression enclosed in square brackets. The transition only fires if this evaluates to true.
An executable atomic calculation or operation executed during the state shift.
Below are detailed UML state machine examples modeling real-world dynamic behavior.
This diagram models the turn-taking flow in a chess game, handling checks, checkmates, and draws.
This traditional UML state model demonstrates state switches during a phone call lifecycle based on user actions and network responses.